<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_special_offers.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_filters.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_top_deals.php";



	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_bookings();

	$template = new template();



// FUNCTION // Show home page
function display_home(){	

		//$bookings = new bookings();
		$holidays = new holidays();

?>
<script>
function form_validator(my_form){

	var valid = false;
	
	if(my_form.reference_id.value == ''){
		alert("You must enter a Holiday Reference Number.");
		return (false);
	}
	
	if(my_form.filter_holiday.checked){
		valid = true;
		return (true);
	}

	if(my_form.mod_price.value !== ''){
		valid = true;
		return (true);
	}
	
	if(my_form.filter_flight.checked){
		valid = true;
		return (true);
	}
	
	if(valid == false){
		alert("You must select an option.");
		return (false);
	}

}
</script>
<h2>Holiday Filter</h2>



<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>


<p><strong>Note:</strong> Only filter out holidays where you are 100% certain that the accommodation/flight is unavailable as once set, the filter cannot be undone.</p>



<div class="border_box">

<fieldset><legend>Holiday Details</legend></fieldset>



<form action="<?= $_SERVER['PHP_SELF']?>" onsubmit="return form_validator(this)" name="filter_form"  method="get">

    
    <input type="hidden" name="action" value="filter_confirm" />
    <input type="hidden" name="tour_op" value="<?= $details['tour_op'] ?>" />

    <table class="libra">
        <tr> 
            <td width="200"><label><strong>Holiday Reference ID</strong></label></td>
            <td><input type="text" name="reference_id" value="<?= $details['reference_id'] ?>" /></td>
        </tr>
    </table>
    
<br />

<fieldset><legend>Filter Options</legend></fieldset>


<table class="libra">
        <tr> 
            <td width="200"><strong>
              <label>Remove this holiday</label>
            </strong></td>
          <td><input type="checkbox" name="filter_holiday" /></td>
        </tr>
        <tr> 
            <td><strong>
              <label>Remove the entire flight</label>
            </strong></td>
          <td><input type="checkbox" name="filter_flight" /></td>
        </tr>
        <tr> 
            <td><strong>
              <label>Modify the &euro; base price</label>
            </strong></td>
          <td>&euro;<input type="text" name="mod_price" value="<?= $filter_values['mod_price'] ?>" style="width: 50px;" /> <span  class='red' ></span></td>
        </tr>
    </table>
    
    <br />

    <input type="submit" value="submit"/>

</form>



</div>

<?
}
// END FUNCTION 







// FUNCTION // Show home page
function display_filter_confirm(){	

	$holiday_references = new holiday_references();
	$holidays = new holidays();
	$holiday_filters = new holiday_filters();
	
	$holiday_values = $holiday_references->get_holiday($_GET['reference_id']);
	 

	// Get prices
	$new_mod_price = array();
	$new_mod_price['price_adult'] = trim(str_replace(".00", "", $_GET['mod_price_adult']));
	$new_mod_price['price_child'] = trim(str_replace(".00", "", $_GET['mod_price_child']));
	
	// See if the price has been filtered BEFORE
	$modified = $holiday_filters->find_holiday_mod_price($_GET['reference_id']);
	
	$effective_price = array();
	
	if(!empty($modified)){
		$mod_price_s = @unserialize($modified[0]['mod_price']);
		if(!$mod_price_s){
			$mod_price['price_adult'] = $modified[0]['mod_price'];
			$effective_price['price_adult'] = $mod_price['price_adult'];
			$effective_price['price_child'] = trim(str_replace(".00", "", $holiday_values['price_child']));
		}else{
			$mod_price = $mod_price_s;
			$effective_price = $mod_price;
		}		
	}else{
		$effective_price['price_adult'] = trim(str_replace(".00", "", $holiday_values['price_adult']));
		$effective_price['price_child'] = trim(str_replace(".00", "", $holiday_values['price_child']));
	}
	
	// get type of filter
	if($_GET['filter_holiday']){
		$type = 'holiday';
	}
	if($_GET['filter_flight']){
		$type = 'flight';
	}
	if($_GET['mod_price_adult']||$_GET['mod_price_child']){
		$type = 'price';
	}	

?>

<h2>Holiday Filter</h2>


<div class="border_box">


<? if($type == 'price'){ ?>

<p>Do you really want to change the <strong>base price</strong> from :</p>
<p>
<strong>Adult Price:</strong> <?= $effective_price['price_adult']; ?> > <?= $new_mod_price['price_adult']; ?><br />
<strong>Child Price:</strong> <?= $effective_price['price_child']; ?> > <?= $new_mod_price['price_child']; ?></p>
</p>
<form action="<? $_SERVER['PHP_SELF']?>" method="POST">

    <input type='hidden' name='type' value='<?= $type ?>' />
    <input type="hidden" name="reference_id" value="<?= $_GET['reference_id'] ?>" />
    <input type="hidden" name="mod_price_adult" value='<?= $new_mod_price['price_adult']; ?>' />
    <input type="hidden" name="mod_price_child" value='<?= $new_mod_price['price_child']; ?>' />
    <input type="hidden" name="action" value="filter" />
    
    <input type="submit" value="Yes" class="form_button">
    <input type="button" value="Cancel" class="form_button" onclick="javascript:history.go(-1)">

</form>


<? } ?>



<? if($type == 'flight'){ ?>

<p>Do you really want to <strong>remove the entire flight and all holidays on this flight</strong> from the site?</p>

<form action="<? $_SERVER['PHP_SELF']?>" method="get">

    <input type='hidden' name='type' value='flight' />
    <input type="hidden" name="reference_id" value="<?= $_GET['reference_id'] ?>" />
    <input type="hidden" name="action" value="filter" />
    
    <input type="submit" value="Yes" class="form_button">
    <input type="button" value="Cancel" class="form_button" onclick="javascript:history.go(-1)">

</form>


<? } ?>


<? if($type == 'holiday'){ ?>

<p>Do you really want to <strong>holiday ref <?= $_GET['reference_id'] ?></strong> from the site?</p>

<form action="<? $_SERVER['PHP_SELF']?>" method="get">

    <input type='hidden' name='type' value='<?= $type ?>' />
    <input type="hidden" name="reference_id" value="<?= $_GET['reference_id'] ?>" />
    <input type="hidden" name="action" value="filter" />
    
    <input type="submit" value="Yes" class="form_button">
    <input type="button" value="Cancel" class="form_button" onclick="javascript:history.go(-1)">

</form>


<? } ?>




</div>

<? 

}



// END FUNCTION 



switch ($_REQUEST['action']){
	default:
		$template->head_booking();
		display_home();
		$template->foot_booking();
	break;
	case "filter_confirm";
		$template->head_booking();
		$holiday_references = new holiday_references();
		$params['reference_id'] = $_GET['reference_id'];
		$holiday_values = $holiday_references->get_holiday($params['reference_id']);
		if($holiday_values){
			display_filter_confirm();
		}else{
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "There was no holiday found with the reference '".$_GET['reference_id']."' ";
			$GLOBALS['note'] = $my_message;
			display_home();
		}
		$template->foot_booking();
	break;
	case "filter";

		$reference_id = $_POST['reference_id'];
		
		$holidays = new holidays();
		$holiday_references = new holiday_references();
		$holiday_filters = new holiday_filters();
		$holiday_top_deals = new holiday_top_deals();
	

		
		
		$holiday_values = $holiday_references->get_holiday($reference_id);
		$holiday_values['reference_id'] = $reference_id;

		if($holiday_values){
			switch ($_POST['type']){
				case "flight";
					$holiday_filters->insert_filtered_flight($holiday_values);
				break;
				case "holiday";
				
					// Insert offer
					$holiday_filters->insert_filtered_holiday($reference_id);
					// remove from top deals
					$holiday_top_deals->delete_top_deals_holiday($reference_id);
					
				break;
				case "price";
				
					$filter_values = array();
					$filter_values_a = array();
					$filter_values['holiday_reference_id'] = $reference_id;
					$filter_values['mod_price'] = json_encode(array('price_adult'=>$_POST['mod_price_adult'],'price_child'=>$_POST['mod_price_child']));
					
					

					if($holiday_filters->find_holiday_mod_price($reference_id)){
						$holiday_filters->update_holiday_mod_price($filter_values);
					}else{
						$holiday_filters->insert_holiday_mod_price($filter_values);
					}
				break;
			}
			
			header("Location: filter.php?action=success");
			
		}else{
			$template->head_booking();
			$my_message = array();
			$my_message['type'] = 'pos';
			$my_message['message'] = "There was no holiday found with the reference '".$_GET['reference_id']."' ";
			$GLOBALS['note'] = $my_message;
			display_home();
			$template->foot_booking();
		}
		
	break;
	case "success";
		$template->head_booking();
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Holiday has been filtered succesfully.";
		$GLOBALS['note'] = $my_message;
		display_home();
		$template->foot_booking();
	break;	
	
	
	
}
	





?>